package com.ruoyi.exam.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.exam.domain.PExam;
import com.ruoyi.exam.domain.PExamQuestion;
import com.ruoyi.exam.domain.PQuestion;
import com.ruoyi.exam.domain.PQuestionTypeEntity;
import com.ruoyi.exam.mapper.PExamMapper;
import com.ruoyi.exam.mapper.PExamQuestionMapper;
import com.ruoyi.exam.mapper.PQuestionMapper;
import com.ruoyi.exam.service.IPExamService;
import com.ruoyi.exam.service.PQuestionTypeService;
import com.ruoyi.exam.vo.AddExamByBankVo;
import com.ruoyi.exam.vo.AddExamByQuestionVo;
import com.ruoyi.exam.vo.ExamByLogResult;
import com.ruoyi.exam.vo.SQuestionResult;
import io.netty.util.internal.ObjectUtil;
import jdk.nashorn.internal.codegen.TypeMap;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 考试Service业务层处理
 *
 * @author ruoyi
 * @date 2022-01-05
 */
@Service
public class PExamServiceImpl implements IPExamService {


    @Resource
    private PExamMapper pExamMapper;

    @Resource
    private PExamQuestionMapper pExamQuestionMapper;

    @Resource
    private PQuestionMapper pQuestionMapper;

    @Resource
    private PQuestionTypeService pQuestionTypeService;

    /**
     * 查询考试
     *
     * @param id 考试主键
     * @return 考试
     */
    @Override
    public PExam selectPExamById(Long id)
    {
        return pExamMapper.selectPExamById(id);
    }

    /**
     * 查询考试列表
     *
     * @param pExam 考试
     * @return 考试
     */
    @Override
    public List<PExam> selectPExamList(PExam pExam)
    {
        return pExamMapper.selectPExamList(pExam);
    }

    /**
     * 新增考试
     *
     * @param pExam 考试
     * @return 结果
     */
    @Override
    public int insertPExam(PExam pExam)
    {
        pExam.setCreateTime(DateUtils.getNowDate());
        return pExamMapper.insertPExam(pExam);
    }

    /**
     * 修改考试
     *
     * @param pExam 考试
     * @return 结果
     */
    @Override
    public int updatePExam(PExam pExam)
    {
        pExam.setUpdateTime(DateUtils.getNowDate());
        return pExamMapper.updatePExam(pExam);
    }

    /**
     * 批量删除考试
     *
     * @param ids 需要删除的考试主键
     * @return 结果
     */
    @Override
    public int deletePExamByIds(Long[] ids)
    {
        return pExamMapper.deletePExamByIds(ids);
    }

    /**
     * 删除考试信息
     *
     * @param id 考试主键
     * @return 结果
     */
    @Override
    public int deletePExamById(Long id)
    {
        return pExamMapper.deletePExamById(id);
    }

  /**
   * 已修改
   * @param addExamByBankVo
   * @param userName
   * @return
   */
  @Override
    public int addExamByBank(AddExamByBankVo addExamByBankVo,String userName){
        SimpleDateFormat strDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        PExam exam = new PExam();
        exam.setCreateBy(userName);
        exam.setCreateTime(new Date());
        exam.setStatus(Long.valueOf(addExamByBankVo.getStatus()));
        exam.setDuration(addExamByBankVo.getExamDuration().toString());
        exam.setTreeTransferLeft(addExamByBankVo.getTreeTransferLeft());
        exam.setTreeTransferRight(addExamByBankVo.getTreeTransferRight());
        exam.setExamDesc(addExamByBankVo.getExamDesc());
        exam.setExamName(addExamByBankVo.getExamName());
        exam.setEquipmentId(addExamByBankVo.getEquipmentId());
        //exam.setPassScore(addExamByBankVo.getPassScore());
        exam.setType(Long.valueOf(addExamByBankVo.getType()));
        //设置密码如果有
        if (addExamByBankVo.getPassword() != null) {
          exam.setPassword(addExamByBankVo.getPassword());
        }
        if (addExamByBankVo.getEndTime() != null && addExamByBankVo.getStartTime() != null) {
          try {
            exam.setEndTime(strDateFormat.parse(addExamByBankVo.getEndTime()));
            exam.setStartTime(strDateFormat.parse(addExamByBankVo.getStartTime()));
          } catch (ParseException e) {
            throw new RuntimeException(e);
          }
        }
        //设置id
//        List<PExam> examList = examService.list(new QueryWrapper<Exam>().orderByDesc("exam_id"));
//        int id = 0;
//        if (examList.size() != 0) {
//            id = examList.get(0).getExamId() + 1;
//        }
        PExamQuestion examQuestion = new PExamQuestion();
        String[] splits = addExamByBankVo.getBankNames().split(",");
        Long[] ids = StringArray2LongArray(splits);
        List<PQuestion> questions = pQuestionMapper.getQuBankIdList(ids);
        String[] allTypeScore = addExamByBankVo.getTitleScore().split("&");
        HashMap<Integer, Integer> tranTypeScore = new HashMap<>();
        for (String typeScore : allTypeScore) {
          tranTypeScore.put(
            Integer.valueOf(typeScore.split("@")[0]),
            Integer.valueOf(typeScore.split("@")[1])
          );
        }
      String[] strings = this.setQuestion(questions, tranTypeScore);

      String[] split = strings[0].split("#");
        examQuestion.setQuestionList(split[0]);//純id
        examQuestion.setQuestionIds(split[1]);//帶題目類型的id  4,20#A1@4&A2@&A3_4@&B@20&X@
        examQuestion.setQuestionScores(strings[1]);
        exam.setTotalScore(Integer.valueOf(strings[2]));
        examQuestion.setScores(strings[3]);
//        String[] s = split[0].split(",");
//        //总分
//        int totalScore = 0;
//        StringBuilder sf = new StringBuilder();
//        if(StringUtils.isNotEmpty(s)){
//          for (String s1 : s) {
//
//              PQuestion question = pQuestionMapper.selectPQuestionById(Long.valueOf(s1));

//              if (question.getQuType() == 1) {
//                sf.append(addExamByBankVo.getA1_Score()).append(",");
//                totalScore += addExamByBankVo.getA1_Score();
//              } else if (question.getQuType() == 2) {
//                sf.append(addExamByBankVo.getX_Score()).append(",");
//                totalScore += addExamByBankVo.getA3_4_Score();
//              } else if (question.getQuType() == 3) {
//                sf.append(addExamByBankVo.getA3_4_Score()).append(",");
//                totalScore += addExamByBankVo.getA3_4_Score();
//              } else if (question.getQuType() == 4) {
//                sf.append(addExamByBankVo.getB_Score()).append(",");
//                totalScore += addExamByBankVo.getB_Score();
//              } else if (question.getQuType() == 5) {
//                sf.append(addExamByBankVo.getA2_Score()).append(",");
//                totalScore += addExamByBankVo.getA2_Score();
//              }
//            }
//
//        }

//        examQuestion.setScores(sf.substring(0, sf.toString().length() - 1));
//        //编辑答案
//        String[] split1 = this.setExamQuestion(questions, split[0], sf.toString().substring(0, sf.toString().length() - 1)).split("#");
//       //目的是为了设置问题得分的字符串拼接1@2,3,4&2@66,67
//        examQuestion.setQuestionScores(split1[1]);~~
//        exam.setTotalScore(totalScore);~~
//
        int success1 = pExamMapper.insertPExam(exam);
        examQuestion.setExamId(exam.getId());
        int success2 = pExamQuestionMapper.insertPExamQuestion(examQuestion);
      return success1 + success2;
    }


  /**
   * 未改
   * @param addExamByQuestionVo
   * @param userName
   * @return
   */
  @Override
    public int addExamByQuestion(AddExamByQuestionVo addExamByQuestionVo,String userName) {
        PExam exam = new PExam();
        exam.setCreateBy(userName);
        exam.setCreateTime(new Date());
        exam.setTotalScore(addExamByQuestionVo.getTotalScore());
        exam.setType(Long.valueOf(addExamByQuestionVo.getType()));
        exam.setPassScore(addExamByQuestionVo.getPassScore());
        exam.setTreeTransferLeft(addExamByQuestionVo.getTreeTransferLeft());
        exam.setTreeTransferRight(addExamByQuestionVo.getTreeTransferRight());
        exam.setExamDesc(addExamByQuestionVo.getExamDesc());
        exam.setExamName(addExamByQuestionVo.getExamName());
        exam.setDuration(addExamByQuestionVo.getExamDuration().toString());
        exam.setEquipmentId(addExamByQuestionVo.getEquipmentId());
        try {
            SimpleDateFormat strDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            if (addExamByQuestionVo.getEndTime() != null){
                exam.setEndTime(strDateFormat.parse(addExamByQuestionVo.getEndTime()));
            }
            if (addExamByQuestionVo.getStartTime() != null) {
                exam.setStartTime(strDateFormat.parse(addExamByQuestionVo.getStartTime()));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        if (addExamByQuestionVo.getPassword() != null) {
            exam.setPassword(addExamByQuestionVo.getPassword());
        }
        exam.setStatus(Long.valueOf(addExamByQuestionVo.getStatus()));
        String scoreStr = addExamByQuestionVo.getScores();
        Integer totalScore = 0;
        for (String score : scoreStr.split(",")) {
          totalScore += Integer.parseInt(score);
        }
        exam.setTotalScore(totalScore);
        int i = pExamMapper.insertPExam(exam);

        PExamQuestion examQuestion = new PExamQuestion();
        examQuestion.setExamId(exam.getId());
        examQuestion.setScores(scoreStr);
        examQuestion.setQuestionList(addExamByQuestionVo.getQuestionIds());
        Long[] ids = StringArray2LongArray(addExamByQuestionVo.getQuestionIds().split(","));
        List<PQuestion> byIds = pQuestionMapper.selectPQuestionByIds(ids);


        String[] strings = this.getQuAScString(byIds,scoreStr);
        String allTypeId = strings[0];
        String titleScore = strings[1];
        examQuestion.setQuestionIds(allTypeId);
        examQuestion.setQuestionScores(titleScore);
        int success = pExamQuestionMapper.insertPExamQuestion(examQuestion);

    return success + i;
    }
    //TODO 更新题目信息，待修改
    @Override
    public int updateExamInfo(AddExamByQuestionVo addExamByQuestionVo) throws ParseException {
      SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
      PExam exam = new PExam();
      exam.setTotalScore(addExamByQuestionVo.getTotalScore());
      exam.setType(Long.valueOf(addExamByQuestionVo.getType()));
      exam.setPassScore(addExamByQuestionVo.getPassScore());
      if (addExamByQuestionVo.getEndTime()!=null){
        exam.setEndTime(formatter.parse(addExamByQuestionVo.getEndTime()));
      }
      if (addExamByQuestionVo.getStartTime()!=null) {
        exam.setStartTime(formatter.parse(addExamByQuestionVo.getStartTime()));
      }
      exam.setExamDesc(addExamByQuestionVo.getExamDesc());
      exam.setExamName(addExamByQuestionVo.getExamName());
      exam.setDuration(addExamByQuestionVo.getExamDuration().toString());
      exam.setTreeTransferRight(addExamByQuestionVo.getTreeTransferRight());
      //设置密码如果有
      if (addExamByQuestionVo.getPassword() != null) {
        exam.setPassword(addExamByQuestionVo.getPassword());
      } else {
        exam.setPassword(null);
      }
      exam.setStatus(Long.valueOf(addExamByQuestionVo.getStatus()));
      exam.setId(Long.valueOf(addExamByQuestionVo.getExamId()));
      String scoreStr = addExamByQuestionVo.getScores();
      Integer totalScore = 0;
      for (String score : scoreStr.split(",")) {
        totalScore += Integer.parseInt(score);
      }
      exam.setTotalScore(totalScore);
      //设置考试的题目和分值信息
      PExamQuestion examQuestion = pExamQuestionMapper.selectPExamQuestionByExamId(Long.valueOf(addExamByQuestionVo.getExamId()));
      examQuestion.setExamId(Long.valueOf(addExamByQuestionVo.getExamId()));
      examQuestion.setScores(addExamByQuestionVo.getScores());
      examQuestion.setQuestionList(addExamByQuestionVo.getQuestionIds());
      Long[] ids = StringArray2LongArray(addExamByQuestionVo.getQuestionIds().split(","));
      List<PQuestion> byIds = pQuestionMapper.selectPQuestionByIds(ids);


      String[] strings = this.getQuAScString(byIds,scoreStr);
      String allTypeId = strings[0];
      String titleScore = strings[1];
      examQuestion.setQuestionIds(allTypeId);
      examQuestion.setQuestionScores(titleScore);
//      String[] strings = this.setQuestion(parentByParentId, new HashMap<>());
//      String[] split = strings[0].split("#");
//      String[] split1 = this.setExamQuestion(parentByParentId, addExamByQuestionVo.getQuestionIds(), addExamByQuestionVo.getScores()).split("#");
//      //题目
//      examQuestion.setQuestionIds(split[1]);
//      //答案
//      examQuestion.setQuestionScores(split1[1]);

      int i = pExamMapper.updatePExam(exam);
      int i1 = pExamQuestionMapper.updatePExamQuestion(examQuestion);

      return i + i1;
    }
    @Override
    public List<PExam> selectPExamByUserList(PExam pExam) {
       return pExamMapper.selectPExamByUserList(pExam);
    }


  /**
   *已修改
   * @param examId
   * @return
   */
  @Override
    public AddExamByQuestionVo getExamById(Integer examId) {
      AddExamByQuestionVo addExamByQuestionVo = new AddExamByQuestionVo();
      SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

      //从表中根据ID查出考试
      PExam exam = pExamMapper.selectPExamById(Long.valueOf(examId));
      addExamByQuestionVo.setExamDesc(exam.getExamDesc());
      addExamByQuestionVo.setExamId(examId);
      addExamByQuestionVo.setExamName(exam.getExamName());
      addExamByQuestionVo.setPassScore(exam.getPassScore());
      addExamByQuestionVo.setTotalScore(exam.getTotalScore());
      addExamByQuestionVo.setExamDuration(exam.getDuration());
      addExamByQuestionVo.setType(Integer.valueOf(exam.getType().toString()));
      addExamByQuestionVo.setPassword(exam.getPassword());
      addExamByQuestionVo.setStatus(Integer.valueOf(exam.getStatus().toString()));
      addExamByQuestionVo.setTreeTransferLeft(exam.getTreeTransferLeft());
      addExamByQuestionVo.setTreeTransferRight(exam.getTreeTransferRight());
      //结束时间
      if (exam.getEndTime()!=null){
        addExamByQuestionVo.setEndTime(formatter.format(exam.getEndTime()));
      }
      //开始时间
      if (exam.getStartTime()!=null){
        addExamByQuestionVo.setStartTime(formatter.format(exam.getStartTime()));
      }
      //查找考试中的所有题目
      PExamQuestion pExamQuestion = pExamQuestionMapper.selectPExamQuestionByExamId(Long.valueOf(examId));
      //查找所有题目类型
      Map<Integer, PQuestionTypeEntity> allQType = getAllQType();
      HashMap<String, Object> MapList = new HashMap<>();
      //获取5个类型题目id
      String questionAllIds = pExamQuestion.getQuestionIds();
      String[] split = questionAllIds.split("&");
      StringBuilder questionIds = new StringBuilder();
      for (String s : split) {
        String[] nameAIds = s.split("@");

        PQuestionTypeEntity pQuestionTypeEntity = allQType.get(Integer.parseInt(nameAIds[0]));
        if(Objects.nonNull(pQuestionTypeEntity)){
          MapList.put(pQuestionTypeEntity.getName(),nameAIds[1]);
          questionIds.append(",");
          questionIds.append(nameAIds[1]);
        }

      }

      addExamByQuestionVo.setQuestionMaps(MapList);
      addExamByQuestionVo.setQuestionIds(questionIds.substring(1,questionIds.length()));
      addExamByQuestionVo.setScores(pExamQuestion.getScores());
      return addExamByQuestionVo;
    }



  @Override
  public  ArrayList<HashMap<String, Object>> examInfo(Long examId) {

    Map<Integer, PQuestionTypeEntity> allQType = getAllQType();
    ArrayList<HashMap<String, Object>> objects = new ArrayList<>();

    PExamQuestion pExamQuestion = pExamQuestionMapper.selectPEQByEId(examId);
    String[] questionlist = pExamQuestion.getQuestionIds().split("&");
    String[] scoreslist = pExamQuestion.getQuestionScores().split("&");

    for (int i = 0; i < questionlist.length; i++) {
      String[] choseAndQuestionId = questionlist[i].split("@"); //1   7,5
      String[] questionids = choseAndQuestionId[1].split(",");//[7,5]
      String[] scoresIds = scoreslist[i].split("@"); //b  18,19,20,21,22
      String[] scoreId = scoresIds[1].split(",");

      PQuestionTypeEntity pQuestionTypeEntity = allQType.get(Integer.parseInt(choseAndQuestionId[0]));//1
      if(Objects.nonNull(pQuestionTypeEntity)){

        List<SQuestionResult> questions = new ArrayList<>();
        for (int j = 0; j < questionids.length; j++) {
          SQuestionResult questionResult = new SQuestionResult();
          questionResult.setId(Integer.valueOf(questionids[j]));
          questionResult.setScore(Long.valueOf(scoreId[j]));
          questions.add(questionResult);
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("type",pQuestionTypeEntity.getType());
        map.put("name",pQuestionTypeEntity.getName());
        map.put("id",pQuestionTypeEntity.getId());
        map.put("questionList",questions);
        objects.add(map);
      }

    }

    return objects;
  }

  @Override
  public ExamByLogResult examLogInfo(ExamByLogResult pExamRecordLog1) {
    Map<Integer, PQuestionTypeEntity> allQType = getAllQType();

    StringBuilder newQuestionIds = new StringBuilder();
    StringBuilder newUserAnswers = new StringBuilder();

    String[] questionIds = pExamRecordLog1.getQuestionIds().split("&");
    //多选@16,48
    // &
    // 单选@7,5
    String[] userAnswers = pExamRecordLog1.getUserAnswers().split("&");//
    for (int i = 0; i < questionIds.length; i++) {
      String[] idANum = questionIds[i].split("@");
      String[] ansANum = userAnswers[i].split("@");
      PQuestionTypeEntity pQuestionTypeEntity = allQType.get(Integer.valueOf(idANum[0]));
      if(Objects.nonNull(pQuestionTypeEntity)){
        newQuestionIds.append("&");
        newQuestionIds.append(pQuestionTypeEntity.getName());
        newQuestionIds.append("@");
        newQuestionIds.append(idANum[1]);

        newUserAnswers.append("&");
        newUserAnswers.append(pQuestionTypeEntity.getName());
        newUserAnswers.append("@");
        newUserAnswers.append(ansANum[1]);
      }
      if(ObjectUtils.isEmpty(newQuestionIds))
        return null;
      pExamRecordLog1.setQuestionIds(newQuestionIds.substring(1));
      pExamRecordLog1.setUserAnswers(newUserAnswers.substring(1));
    }

    return pExamRecordLog1;
  }

    private Map<Integer, PQuestionTypeEntity> getAllQType(){
      Map<Integer, PQuestionTypeEntity> dict = new HashMap<>();
      List<PQuestionTypeEntity> pQuestionTypeEntities = pQuestionTypeService.selectAll();
      for (PQuestionTypeEntity pQuestionTypeEntity : pQuestionTypeEntities) {
        dict.put(pQuestionTypeEntity.getId(),pQuestionTypeEntity);
      }
      return dict;
    }

  private String[] setQuestion(List<PQuestion> pQuestions,Map<Integer,Integer> tranTypeScore){
    //"1@1
    // 2@1
    // 18@1
    // 19@1
    // 21@1
    // 31@1"

    Map<Integer, PQuestionTypeEntity> allQType = getAllQType();//"4,20#A1@4&A2@&A3_4@&B@20&X@"
    //这里是将题目按照题目类型进行分组
    Map<Long, List<PQuestion>> allQuestionsByType = pQuestions
      .stream()
      .collect(Collectors.groupingBy(PQuestion::getQuType));

    StringBuilder allTypeId = new StringBuilder();
    StringBuilder ids = new StringBuilder();
    Integer totalScore = 0;
    StringBuilder titleScore = new StringBuilder();
    StringBuilder scores = new StringBuilder();

    //遍历每一种题目类型的题目
    for (Map.Entry<Long, List<PQuestion>> longListEntry : allQuestionsByType.entrySet()) {
      Long type = longListEntry.getKey();
      List<PQuestion> pQuestionsBT = longListEntry.getValue();

      PQuestionTypeEntity pQuestionTypeEntity = allQType.get(type.intValue());
      if(Objects.nonNull(pQuestionTypeEntity)){
        allTypeId.append(pQuestionTypeEntity.getId());
        allTypeId.append("@");

        String id = pQuestionsBT
          .stream()
          .map(PQuestion::getId)
          .collect(Collectors.toList())
          .toString().replaceAll(" ","");
        id = id.substring(1,id.length()-1);
        ids.append(id);
        ids.append(",");
        allTypeId.append(id);
        allTypeId.append("&");
        //以下是对分数的处理
        if(!tranTypeScore.isEmpty()){
          Integer score = tranTypeScore.get(pQuestionTypeEntity.getId());
          if(score != null){
            totalScore += score;
            titleScore.append("&");
            titleScore.append(type);
            titleScore.append("@");
            titleScore.append(score);
            scores.append(",");
            scores.append(score);
          }
        }
      }
    }
    String questionIds = ids.substring(0,ids.length()-1) +"#" +allTypeId.substring(0,allTypeId.length()-1);

    if(!tranTypeScore.isEmpty()){
      String[] strings = {questionIds,titleScore.substring(1),totalScore.toString(),scores.substring(1)};
      return strings;
    }else {
      String[] strings = {questionIds};
      return strings;
    }

    //字段转换
//    private String setQuestion(List<PQuestion> pQuestions){
//
//      Map<Integer, PQuestionTypeEntity> allQType = getAllQType();//"4,20#A1@4&A2@&A3_4@&B@20&X@"
//      Map<Long, List<PQuestion>> allQuestionsByType = pQuestions
//        .stream()
//        .collect(Collectors.groupingBy(PQuestion::getQuType));
//
//      StringBuilder allTypeId = new StringBuilder();
//      StringBuilder ids = new StringBuilder();
//
//      allQuestionsByType.forEach((type,pQuestionsBT)->{
//        PQuestionTypeEntity pQuestionTypeEntity = allQType.get(type.intValue());
//        if(Objects.nonNull(pQuestionTypeEntity)){
//          allTypeId.append(pQuestionTypeEntity.getId());
//          allTypeId.append("@");
//
//          String id = pQuestionsBT
//            .stream()
//            .map(PQuestion::getId)
//            .collect(Collectors.toList())
//            .toString();
//          id = id.substring(1,id.length()-1);
//          ids.append(id);
//          ids.append(",");
//          allTypeId.append(id);
//          allTypeId.append("&");
//        }
//
//      });
//
//      return ids.substring(0,ids.length()-1) + allTypeId.substring(0,allTypeId.length()-1);
//       ids = pQuestions
//        .stream()
//        .map(PQuestion::getId)
//        .collect(Collectors.toList())
//        .toString();
//      ids = ids.substring(1,ids.length()-1);


//      List<Integer> setA1 = new ArrayList<Integer>();
//        List<Integer>  setA2 = new ArrayList<Integer>();
//        List<Integer>  setA3_4 = new ArrayList<Integer>();
//        List<Integer>  setB = new ArrayList<Integer>();
//        List<Integer>  setX = new ArrayList<Integer>();
//        for (PQuestion questions: parentByParentId) {
//            if(questions.getQuType() == 1){
//                setA1.add(Integer.valueOf(questions.getId().toString()));
//            }else if (questions.getQuType() == 5){
//                setA2.add(Integer.valueOf(questions.getId().toString()));
//            }else if (questions.getQuType() == 3){
//                setA3_4.add(Integer.valueOf(questions.getId().toString()));
//            }else if (questions.getQuType() == 4){
//                setB.add(Integer.valueOf(questions.getId().toString()));
//            }else if (questions.getQuType() == 2){
//                setX.add(Integer.valueOf(questions.getId().toString()));
//            }
//        }
//        String quIdA1 ="";
//        String quIdA2 ="";
//        String quIdA3_4 ="";
//        String quIdB = "";
//        String quIdX ="";
//        String quIds="";
//        String quId_B="";
//        if(setA1.size() >0){
//            quIdA1 = setA1.toString().substring(1, setA1.toString().length() - 1)
//                    .replaceAll(" ", "");
//            quIds+=quIdA1;
//        }
//        if(setA2.size() >0){
//            quIdA2 = setA2.toString().substring(1, setA2.toString().length() - 1)
//                    .replaceAll(" ", "");
//            if(quIds !=null && !quIds.equals("")){
//                quIds+=","+quIdA2;
//            }else{
//                quIds+= quIdA2;
//            }
//        }
//        if(setA3_4.size() >0){
//            quIdA3_4 = setA3_4.toString().substring(1, setA3_4.toString().length() - 1)
//                    .replaceAll(" ", "");
//            if(quIds !=null && !quIds.equals("")){
//                quIds+=","+quIdA3_4;
//            }else{
//                quIds+= quIdA3_4;
//            }
//        }
//        if(setB.size() >0){
//            quIdB = setB.toString().substring(1, setB.toString().length() - 1)
//                    .replaceAll(" ", "");
//            if(quIds !=null && !quIds.equals("")){
//                quIds+=","+quIdB;
//            }else{
//                quIds+= quIdB;
//            }
//        }
//        if(setX.size() >0){
//            quIdX = setX.toString().substring(1, setX.toString().length() - 1)
//                    .replaceAll(" ", "");
//            if(quIds !=null && !quIds.equals("")){
//                quIds+=","+quIdX;
//            }else{
//                quIds+= quIdX;
//            }
//        }
//
//        String quIdsall = "A1@"+quIdA1+"&A2@"+quIdA2 +"&A3_4@"+ quIdA3_4+"&B@"+ quIdB+"&X@"+quIdX;
//        quIds +="#"+quIdsall;
//        return quIds;//"4,20#A1@4&A2@&A3_4@&B@20&X@"
    }

    //字段转换  答案 String quIdsall = "A1@"+quIdA1+"&A2@"+quIdA2 +"&A3_4@"+ quIdA3_4+"&B@"+ quIdB+"&X@"+quIdX;
    private String setExamQuestion(List<PQuestion> parentByParentId,String ids,String scores){
        Long[] idsL = StringArray2LongArray(ids.split(","));
        String[] scoresL = scores.split(",");
        List<String> examScores = new ArrayList<String>();

        for (PQuestion pq:parentByParentId) {
            for(int i = 0 ; i < idsL.length;i++)
            {
                if(idsL[i] == pq.getId())
                {
                    examScores.add(scoresL[i]);
                }
            }
        }

        List<String> setA1 = new ArrayList<String>();
        List<String>  setA2 = new ArrayList<String>();
        List<String>  setA3_4 = new ArrayList<String>();
        List<String>  setB = new ArrayList<String>();
        List<String>  setX = new ArrayList<String>();
        for(int i = 0; i < parentByParentId.size() ; i++)
        {
            if(parentByParentId.get(i).getQuType() == 1){
                setA1.add(examScores.get(i));
            }else if (parentByParentId.get(i).getQuType() == 5){
                setA2.add(examScores.get(i));
            }else if (parentByParentId.get(i).getQuType() == 3){
                setA3_4.add(examScores.get(i));
            }else if (parentByParentId.get(i).getQuType() == 4){
                setB.add(examScores.get(i));
            }else if (parentByParentId.get(i).getQuType() == 2){
                setX.add(examScores.get(i));
            }
        }

        String quIdA1 ="";
        String quIdA2 ="";
        String quIdA3_4 ="";
        String quIdB = "";
        String quIdX ="";
        String quIds="";
        String quId_B="";
        if(setA1.size() >0){
            quIdA1 = setA1.toString().substring(1, setA1.toString().length() - 1)
                    .replaceAll(" ", "");
            quIds+=quIdA1;
        }
        if(setA2.size() >0){
            quIdA2 = setA2.toString().substring(1, setA2.toString().length() - 1)
                    .replaceAll(" ", "");
            if(quIds !=null && !quIds.equals("")){
                quIds+=","+quIdA2;
            }else{
                quIds+= quIdA2;
            }
        }
        if(setA3_4.size() >0){
            quIdA3_4 = setA3_4.toString().substring(1, setA3_4.toString().length() - 1)
                    .replaceAll(" ", "");
            if(quIds !=null && !quIds.equals("")){
                quIds+=","+quIdA3_4;
            }else{
                quIds+= quIdA3_4;
            }
        }
        if(setB.size() >0){
            quIdB = setB.toString().substring(1, setB.toString().length() - 1)
                    .replaceAll(" ", "");
            if(quIds !=null && !quIds.equals("")){
                quIds+=","+quIdB;
            }else{
                quIds+= quIdB;
            }
        }
        if(setX.size() >0){
            quIdX = setX.toString().substring(1, setX.toString().length() - 1)
                    .replaceAll(" ", "");
            if(quIds !=null && !quIds.equals("")){
                quIds+=","+quIdX;
            }else{
                quIds+= quIdX;
            }
        }

        String quIdsall = "A1@"+quIdA1+"&A2@"+quIdA2 +"&A3_4@"+ quIdA3_4+"&B@"+ quIdB+"&X@"+quIdX;
        quIds +="#"+quIdsall;
        return quIds;
    }

    //数组转换
    public static Long[] StringArray2LongArray(String[] stringArray) {
        List<Long> list = new ArrayList<>();
        for (String str : stringArray) {
            try {
                list.add(Long.parseLong(str));
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        Long[] longArray = list.toArray(new Long[list.size()]);
        return longArray;
    }

  @Override
  public String[] getQuAScString(List<PQuestion> pQuestions, String score) {
    Map<Integer, PQuestionTypeEntity> allQType = getAllQType();//"4,20#A1@4&A2@&A3_4@&B@20&X@"
    //这里是将题目按照题目类型进行分组
    String[] scores = score.split(",");
    for (int i = 0; i < pQuestions.size(); i++) {
      pQuestions.get(i).setScore(Integer.valueOf(scores[i]));
    }

    Map<Long, List<PQuestion>> allQuestionsByType = pQuestions
      .stream()
      .collect(Collectors.groupingBy(PQuestion::getQuType));

    StringBuilder allTypeId = new StringBuilder();
    StringBuilder titleScore = new StringBuilder();

    //遍历每一种题目类型的题目
    for (Map.Entry<Long, List<PQuestion>> longListEntry : allQuestionsByType.entrySet()) {
      Long type = longListEntry.getKey();
      List<PQuestion> pQuestionsBT = longListEntry.getValue();

      PQuestionTypeEntity pQuestionTypeEntity = allQType.get(type.intValue());
      if(Objects.nonNull(pQuestionTypeEntity)){
        allTypeId.append(pQuestionTypeEntity.getId());
        allTypeId.append("@");

        String id = pQuestionsBT
          .stream()
          .map(PQuestion::getId)
          .collect(Collectors.toList())
          .toString()
          .replaceAll(" ","");

        id = id.substring(1,id.length()-1);
        allTypeId.append(id);
        allTypeId.append("&");
        //以下是对分数的处理
        titleScore.append("&");
        titleScore.append(type);
        titleScore.append("@");

        int typeScore = 0;
        for (PQuestion qu : pQuestionsBT){
          typeScore += qu.getScore();
        }
        titleScore.append(typeScore);
      }
    }
    String[] strings = {allTypeId.substring(0,allTypeId.length()-1),titleScore.substring(1)};
    return strings;


  }
}
